/**
 * Copyright (c) 2008-2016 by DHCC
 * All rights reserved.
 */

package com.dhcc.android.base.db.Sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务进度监控数据库管理
 * @author zhoudan
 * @date 2016年4月26日
 */
public class RecordDBManager {

    private DBHelper helper;
    private SQLiteDatabase db; 
     
    private String table = "TASK_RECORD";
    
    public RecordDBManager(Context context){
        helper = new DBHelper(context);
        db = helper.getWritableDatabase();
        
        db.execSQL("CREATE TABLE IF NOT EXISTS "+table+" (ID VARCHAR PRIMARY KEY  NOT NULL , UUID VARCHAR, PUUID VARCHAR"
                + ", TIME VARCHAR, OPERATOR VARCHAR, OPERATION VARCHAR, TABLEID VARCHAR, UPLOAD VARCHAR, BZ VARCHAR);");
    }
    
    /**
     * 添加进度记录
     */
    public boolean add(String uuid,String puuid,String operation,String tableid){
        db.beginTransaction();
        try{
            ContentValues cv = new ContentValues();
            cv.put("UPLOAD", "0");
            cv.put("TIME", StringUtil.formatDateTimeToString());
            cv.put("OPERATOR", Constants.USERID);
            cv.put("BZ", Constants.currentCycle.getLabel());
            cv.put("UUID", uuid);
            cv.put("PUUID", puuid);
            cv.put("OPERATION", operation);
            cv.put("TABLEID", tableid);
            cv.put("ID", CommenUtil.createUUID());
            
            db.insert(table, null, cv);
            
            db.setTransactionSuccessful();
        }finally{
            db.endTransaction();
            db.close();
        }
        
        
        return true;
    }
    
    /**
     * 添加进度记录
     */
    public boolean add(List<Map<String, String>> dklist,String operation,String tableid){
        db.beginTransaction();
        try{
            ContentValues cv = new ContentValues();
            cv.put("UPLOAD", "0");
            cv.put("TIME",StringUtil.formatDateTimeToString());
            cv.put("OPERATOR", Constants.USERID);
            cv.put("BZ", Constants.currentCycle.getLabel());
            cv.put("OPERATION", operation);
            cv.put("TABLEID", tableid);
            for (Map<String, String> dk : dklist) {
                cv.put("ID", CommenUtil.createUUID());
                cv.put("UUID", dk.get("DKBHU"));
                cv.put("PUUID", dk.get("YFBHU"));
                
                db.insert(table, null, cv);
            }
            
            db.setTransactionSuccessful();
        }finally{
            db.endTransaction();
            db.close();
        }
        
        
        return true;
    }
    
    /**
     * 上传成功后将本地记录标识从0改为1
     */
    public boolean upload(){
        db.beginTransaction();
        try{
            ContentValues cv = new ContentValues();
            cv.put("UPLOAD", "1");
            
            db.update(table, cv, "UPLOAD = ?", new String[]{"0"});
            
            db.setTransactionSuccessful();
        }finally{
            db.endTransaction();
            db.close();
        }
        
        return true;
    }
    
    /**
     * 判断本地是否有未上传的进度记录
     */
    public boolean isNeedUpload(){
        int n = 0;
        String sql = "select count(*) as num from "+table + " where UPLOAD = '0'";
        Cursor c = db.rawQuery(sql, null);
        if(c.moveToFirst()){
            n = c.getInt(c.getColumnIndex("num"));
        }
        
        if(n > 0){
            return true;
        }
        
        return false;
    }
    
    
    /**
     * 获取需要上传的进度记录集合
     */
    public List<Map<String, String>> getUploadList(){
        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
        String sql = "select * from "+table + " where UPLOAD = '0'";
        Cursor c = db.rawQuery(sql, null);
        while(c.moveToNext()){
            Map<String, String> map = new HashMap<String, String>();
            map.put("UUID", c.getString(c.getColumnIndex("UUID")));
            map.put("PUUID", c.getString(c.getColumnIndex("PUUID")));
            map.put("TIME", c.getString(c.getColumnIndex("TIME")));
            map.put("OPERATOR", c.getString(c.getColumnIndex("OPERATOR")));
            map.put("OPERATION", c.getString(c.getColumnIndex("OPERATION")));
            map.put("TABLEID", c.getString(c.getColumnIndex("TABLEID")));
            map.put("BZ", c.getString(c.getColumnIndex("BZ")));
            
            list.add(map);
        }
        c.close();
        db.close();
        return list;
    }
    
}
